<template>
  <div class="page-view">
    <component
      :is="headerComp"
      :rate="grade"
    >
      <template>
        <template slot="right">
          得分<em class="score">
            {{ $route.query.TotalScore }}
          </em>分
        </template>
        <template slot="total">
          共{{ $route.query.PaperTotalScore }}分
        </template>
      </template>
    </component>
    <div class="result-title van-hairline--bottom">
      <!-- <img
        src="~assets/exam/noted.png"
        class="icon"
      > -->
      <span>答题卡</span>
    </div>
    <TestList
      :questions="questions"
      :show-correctness="true"
    />
    <div class="result-footer">
      <div
        class="all btn"
        @click="onShowAnswer('0')"
      >
        查看解析
      </div>
      <div
        v-if="hasRedirect"
        class="sep"
      />
      <div
        v-if="hasRedirect"
        class="all btn"
        @click="returnRedirect"
      >
        返回继续学习
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { mapState } from 'vuex'
import { MappedState } from '@/mixins/testSubmit'
export default Vue.extend({
  components: {
    TestList: () => import('components/Exam/TestList.vue'),
    TestResultHeaderH5: () => import('components/Exam/TestResultHeaderH5.vue'),
  },
  data () {
    return {
      grade: 0,
      questions: [],
      search: '',
      headerComp: 'TestResultHeaderH5',
      redirectInfo: {} as any,
      hasRedirect: false,
    }
  },
  activated () {
    this.setData()
    if (this.search !== location.search) {
      this.search = location.search
      this.$set(this.$data, 'questions', [...this.$store.state.question.QuestionList])
    }
    const fromPage = sessionStorage.getItem('paperFromPage')
    if (fromPage) {
      this.hasRedirect = true
      this.redirectInfo = JSON.parse(fromPage)
    }
  },
  methods: {
    returnRedirect () {
      if (!this.hasRedirect) {
        return
      }
      sessionStorage.removeItem('paperFromPage')
      const { from, query } = this.redirectInfo
      this.$router.replace({ name: from as string, query })
    },
    setData () {
      let score = +this.$route.query.TotalScore
      let totalScore = +this.$route.query.PaperTotalScore
      this.grade = totalScore ? score / totalScore : 0
    },
    onShowAnswer (ShowType: string) {
      window.questionIndex = 0
      this.$router.push({
        name: 'examTest',
        query: {
          ...this.$route.query,
          showAnswer: '1',
          ShowType,
          fromResult: '1',
          fromApp: '',
        },
      })
    },
  },
})
</script>

<style lang="scss" scoped>
$footerHeight: 98px;
.page-view {
  padding-bottom: $footerHeight;
  background: #fff;
  min-height: 100%;
}
.result-title {
  display: flex;
  align-items: center;
  // height: 68px;
  padding: 30px 26px;
  font-size: 28px;
  color: #333;
  line-height: 1;
  + .list {
    padding-top: 0;
  }
}
.result-footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: $footerHeight;
  // padding: 0 50px;
  display: flex;
  align-items: center;
  background: #fff;
  @include px1border(top, #d9d9d9);
  .sep {
    width: 1px;
    height: 66px;
    @include px1border(right, #d9d9d9);
  }
  .btn {
    flex: 1;
    height: $footerHeight;
    font-size: 32px;
    @include active;
    @include flex-middle;
    justify-content: center;
    color: $primary-web;
  }
  .wrong {
    color: $error-web;
  }
}
</style>
